package com.yanqu.road.dao.impl.activity.huashigang;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangAttackRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class HuaShiGangAttackRecordDaoImpl extends TempDataObjectDao<HuaShiGangAttackRecord> {
    public static String getHuaShiGangAttackRecordSql = "select * from t_c_huashigang_attack_record where activityId=?";

    public Map<String, Map<Integer, Map<Long, HuaShiGangAttackRecord>>> getHuaShiGangAttackRecord(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, Map<Long, HuaShiGangAttackRecord>>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaShiGangAttackRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangAttackRecord huaShiGangAttackRecord = parse(rs);
                if (!map.containsKey(huaShiGangAttackRecord.getPointKey())) {
                    map.put(huaShiGangAttackRecord.getPointKey(), new ConcurrentHashMap<>());
                }

                if (!map.get(huaShiGangAttackRecord.getPointKey()).containsKey(huaShiGangAttackRecord.getMonsterId())) {
                    map.get(huaShiGangAttackRecord.getPointKey()).put(huaShiGangAttackRecord.getMonsterId(), new ConcurrentHashMap<>());
                }
                map.get(huaShiGangAttackRecord.getPointKey()).get(huaShiGangAttackRecord.getMonsterId()).put(huaShiGangAttackRecord.getUserId(), huaShiGangAttackRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHuaShiGangAttackRecordSql = "update t_c_huashigang_attack_record set userBaseInfo=?,damage=?,updateTime=? where activityId=? and pointKey=? and monsterId=? and serverId=? and userId=?";

    @Override
    protected boolean updateData(HuaShiGangAttackRecord huaShiGangAttackRecord) {
        huaShiGangAttackRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangAttackRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangAttackRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangAttackRecord.getPointKey()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangAttackRecord.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getUserId()));
        if (getDb().executeNoneQuery(updateHuaShiGangAttackRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangAttackRecordSql = "insert into t_c_huashigang_attack_record(activityId,pointKey,monsterId,serverId,userId,userBaseInfo,damage,updateTime) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HuaShiGangAttackRecord huaShiGangAttackRecord) {
        huaShiGangAttackRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangAttackRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangAttackRecord.getPointKey()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangAttackRecord.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangAttackRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangAttackRecord.getUpdateTime()));
        if (getDb().executeNoneQuery(insertHuaShiGangAttackRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangAttackRecord parseData(ResultSet rs) throws SQLException {
        HuaShiGangAttackRecord huaShiGangAttackRecord = new HuaShiGangAttackRecord();
        huaShiGangAttackRecord.setActivityId(rs.getInt("activityId"));
        huaShiGangAttackRecord.setPointKey(rs.getString("pointKey"));
        huaShiGangAttackRecord.setMonsterId(rs.getInt("monsterId"));
        huaShiGangAttackRecord.setServerId(rs.getLong("serverId"));
        huaShiGangAttackRecord.setUserId(rs.getLong("userId"));
        huaShiGangAttackRecord.setUserBaseInfo(JSONObject.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        huaShiGangAttackRecord.setDamage(rs.getLong("damage"));
        huaShiGangAttackRecord.setUpdateTime(rs.getLong("updateTime"));

        return huaShiGangAttackRecord;
    }
}
